# -*- coding: utf-8 -*-
# @Author	: starview.guojian.ma
# @Date		: 2021/12/1 11:09
# @Last Modified by:   starview.guojian.ma
# @Last Modified time: 2021/12/1 11:09
# Thanks for your comments!
import json
import logging

import asyncio
import traceback
from json import JSONDecodeError

from rabbitmq_request_response.amqplib import RabbitMQFactory

logger = logging.getLogger(__name__)


async def mq_rpc(virtual_host, exchange_name, rk, msg, timeout=100):
    try:
        mq_factory = RabbitMQFactory()
        ret = await asyncio.wait_for(mq_factory.query_mq(virtual_host, exchange_name, rk, msg), timeout=timeout)
        result = json.loads(ret.body.decode('utf-8'))
    except asyncio.TimeoutError as te:
        logger.error(traceback.format_exc())
        return None
    except JSONDecodeError as e:
        logger.error(traceback.format_exc())
        return None
    else:
        return result
